﻿using System; using HISModels.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KeanuMvc.Reference;
using HIS.Utils;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using HISModels.Entities;
using System.Configuration;
using HIS.Utils.Securities;

namespace HIS.Controllers.Master {
    public class HospitalUnit : HIS.Controllers.AppBaseController{
        
        #region Fields
        private string strAction = string.Empty;
        private string strNotification = string.Empty;
        private RefHu oHospitalUnit = null;
        private Address oHospitalAddress = null;
        private HuWard oWard = null;
        private HuRoom oHuRoom= null;
        private HuBed oHuBed = null;
        #endregion

        #region Constructors
        public HospitalUnit() {
            Data.Add("Pagetitle", "Hospital Unit");
            Data.Add("Notification", strNotification);
        }

        #endregion
        [Authorization(RoleAction = "all")]
        public override void Index() {
            try {
                Data["Pagetitle"] += " :: Hospital Unit List";
                try{
                    HttpContext.Current.Session.Remove("HospitalUnitId");
                    HttpContext.Current.Session.Remove("DdLobRel");
                    HttpContext.Current.Session.Remove("hospitalName");
                }catch{}
                
                RenderFile("master\\hospitalunits\\hospitalunits.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "all")]
        public void TableList() {
            StringReader stringReader = null;
            string strJson = string.Empty;

            try {
                string TextboxHospitalCode = HttpContext.Current.Request["TextboxHospitalCode"].NullSafeToString();
                string TextboxHospitalUnitName = HttpContext.Current.Request["TextboxHospitalUnitName"].NullSafeToString();
                string ssearch = "TextboxHospitalCode=" + TextboxHospitalCode + ";TextboxHospitalUnitName=" + TextboxHospitalUnitName;
                ///--------- Sample post to rest and get record as paging-------------/// 
                ///Create paging object///
                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                if (!string.IsNullOrEmpty(ssearch)) {
                    pagingSearch.sSearch = ssearch;
                } else { pagingSearch.sSearch = Convert.ToString(HttpContext.Current.Request["sSearch"]); }

                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);

                ///Set Rest Url///
                string strUri = Global.ServiceUriDictionary["hospitalpage"] + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                //strJson = stringReader.ReadToEnd();
                
                try {
                    JObject o = JObject.Parse(stringReader.ReadToEnd());
                    JArray aaData = (JArray)o["aaData"];
                    JArray aaDataModified = new JArray();

                    for (int i = 0; i < aaData.Count(); i++) {
                        JObject oo = JObject.Parse(aaData[i].ToString());
                        oo["hospitalencrpt"] = KeanuMvc.Reference.CryptoString.Encrypt((oo["Id"] + ";" +
                                             oo["NameValue"]).ToString());
                        aaDataModified.Add(oo);
                    }
                    o["aaData"] = aaDataModified;
                    strJson = JsonConvert.SerializeObject(o);
                } catch {}
              
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            } finally {
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
                HttpContext.Current.Response.Write(strJson);
            }
        }

        [Authorization(RoleAction = "all")]
        public void CreateHospitalUnit() {
            ///Set variables///
            StringReader stringReader = null;

            try {
                oHospitalUnit = new RefHu(); HIS.Utils.Helper.SetNullToEmptyString(oHospitalUnit);
                oHospitalAddress = new Address(); HIS.Utils.Helper.SetNullToEmptyString(oHospitalAddress);
                Data.Add("oHospitalUnit", oHospitalUnit);
                Data.Add("oHospitalAddress", oHospitalAddress);
                Data.Add("IsCreate", "true");

                Data["Pagetitle"] += " :: Create Hospital Unit";
                Data["Notification"] = strNotification;
            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("master\\hospitalunits\\edithospitalunits.html");
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void EditHospitalUnit() {
            ///Set variables///
            StringReader stringReader = null;
            int id = 0;
            try {
                string hospitalencrpt = HttpContext.Current.Request["hospitalencrpt"].NullSafeToString();
                if (!string.IsNullOrEmpty(hospitalencrpt)) {
                    string hospitaldecript = KeanuMvc.Reference.CryptoString.Decrypt(hospitalencrpt);
                    string[] strhospital = hospitaldecript.Split(';');
                    id = Convert.ToInt32(strhospital[0]);
                } 
            } catch {}

            try {
                oHospitalUnit= new RefHu();
                oHospitalAddress = new Address(); Utils.Helper.SetNullToEmptyString(oHospitalAddress);

                string strUri = Global.ServiceUriDictionary["hospital"] +id+ Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.GetJson(strUri);
                oHospitalUnit = JsonConvert.DeserializeObject<RefHu>(stringReader.ReadToEnd());
                Utils.Helper.SetNullToEmptyString(oHospitalUnit);
                if (oHospitalUnit.Addresses.Count > 0) {
                    foreach (var item in oHospitalUnit.Addresses) {
                        oHospitalAddress = item;
                    }
                } 
                Data.Add("oHospitalAddress", oHospitalAddress);
                Data.Add("oHospitalUnit", oHospitalUnit);
                
                Data.Add("IsCreate", "false");

                Data["Pagetitle"] += " :: Edit Hospital Unit";
                Data["Notification"] = strNotification;
            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("master\\hospitalunits\\edithospitalunits.html");
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void SaveHospitalUnitToService() {
            StringReader stringReader = null;
            string strUri = string.Empty;
            try {
                oHospitalUnit = new RefHu(); Utils.Helper.SetNullToEmptyString(oHospitalUnit);
                oHospitalAddress= new Address(); Utils.Helper.SetNullToEmptyString(oHospitalAddress);
                if (HttpContext.Current.Request["IsCreate"] != "true") {
                    int Id = 0;
                    try {
                        Id = Convert.ToInt32(HttpContext.Current.Request["Id"]);
                    } catch { }
                    //flag
                    strUri = Global.ServiceUriDictionary["hospital"] + Id + Global.ServiceUriDictionary["requestformat"];
                    ///Get Rest///
                    stringReader = base.RestService.GetJson(strUri);
                    oHospitalUnit = JsonConvert.DeserializeObject<RefHu>(stringReader.ReadToEnd());
                }

                oHospitalUnit.PostHandler<RefHu>();
                oHospitalAddress.PostHandler<Address>();
                oHospitalAddress.TypeId = 1;
                if (oHospitalUnit.Addresses.Count == 0) {
                    oHospitalUnit.Addresses.Add(oHospitalAddress);
                }
                

                strUri = Global.ServiceUriDictionary["hospital"] + Global.ServiceUriDictionary["requestformat"];
                if (HttpContext.Current.Request["IsCreate"] == "true") {
                    stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(oHospitalUnit));
                } else {
                    stringReader = base.RestService.PutJson(strUri, JsonConvert.SerializeObject(oHospitalUnit));
                }
                
                oHospitalUnit = JsonConvert.DeserializeObject<RefHu>(stringReader.ReadToEnd());

                if (oHospitalUnit != null) {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                } else {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }

                strNotification = "{\"strNotification\": \"" + strNotification + "\"}";

            } catch (Exception ex) {
                strNotification = ex.Message;
            } finally {
                HttpContext.Current.Response.Write(strNotification);
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void DeleteHospitalUnit() {
            ///Set variables///
            StringReader stringReader = null;
            int id = 0;
            try {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            } catch { }

            try {
                string strUri = Global.ServiceUriDictionary["hospital"]+ Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.DeleteJson(strUri, JsonConvert.SerializeObject(id));

                try {
                    oHospitalUnit = JsonConvert.DeserializeObject<RefHu>(stringReader.ReadToEnd());
                } catch {}

                if (oHospitalUnit != null) {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                } else {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }

            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                HttpContext.Current.Response.Write(strNotification);
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void SetHospitalUnit() {
            HttpContext.Current.Session.Remove("WardId");
            HttpContext.Current.Session.Remove("WardName");
            int id = 0;
            string hospitalName = string.Empty;

            if (HttpContext.Current.Session["HospitalUnitId"] != null) {
                id = Convert.ToInt32(HttpContext.Current.Session["HospitalUnitId"]);
                hospitalName = Convert.ToString(HttpContext.Current.Session["hospitalName"]);
            } else {
                string hospitalencrpt = HttpContext.Current.Request["hospitalencrpt"].NullSafeToString();
                if (!string.IsNullOrEmpty(hospitalencrpt)) {
                    string hospitaldecript = KeanuMvc.Reference.CryptoString.Decrypt(hospitalencrpt);
                    string[] strhospital = hospitaldecript.Split(';');
                    id =Convert.ToInt32(strhospital[0]);
                    hospitalName = strhospital[1];
                } 
            }
            
            try {
                Data["Pagetitle"] = "Hospital Unit Ward";
                
                Data.Add("HospitalUnitId",id);
                Data.Add("hospitalName", hospitalName);
                
                HttpContext.Current.Session.Add("HospitalUnitId", id);
                HttpContext.Current.Session.Add("hospitalName", hospitalName);
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }finally{
                RenderFile("master\\hospitalunits\\viewward.html");
            }
        }

        
        [Authorization(RoleAction = "all")]
        public void TableListWard() {
            StringReader stringReader = null;
            string strJson = string.Empty;

            try {
                string TextboxHospitalUnitId = HttpContext.Current.Request["TextboxHospitalUnitId"].NullSafeToString();
                string TextboxWard = HttpContext.Current.Request["TextboxWard"].NullSafeToString();
                string ssearch = "TextboxHospitalUnitId=" + TextboxHospitalUnitId + ";TextboxWard=" + TextboxWard;
                ///--------- Sample post to rest and get record as paging-------------/// 
                ///Create paging object///
                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                if (!string.IsNullOrEmpty(ssearch)) {
                    pagingSearch.sSearch = ssearch;
                } else { pagingSearch.sSearch = Convert.ToString(HttpContext.Current.Request["sSearch"]); }

                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                string strUri = Global.ServiceUriDictionary["wardhospitalpage"] + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                //strJson = stringReader.ReadToEnd();

                try {
                    JObject o = JObject.Parse(stringReader.ReadToEnd());
                    JArray aaData = (JArray)o["aaData"];
                    JArray aaDataModified = new JArray();

                    for (int i = 0; i < aaData.Count(); i++) {
                        JObject oo = JObject.Parse(aaData[i].ToString());
                        oo["wardencrpt"] = KeanuMvc.Reference.CryptoString.Encrypt((oo["Id"] + ";" +
                                             oo["NameValue"]).ToString());
                        aaDataModified.Add(oo);
                    }
                    o["aaData"] = aaDataModified;
                    strJson = JsonConvert.SerializeObject(o);
                } catch { }

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            } finally {
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
                HttpContext.Current.Response.Write(strJson);
            }
        }

        [Authorization(RoleAction = "all")]
        public void CreateWard() {
            StringReader stringReader = null;
            string hospitalName = string.Empty;
            int id = 0;
            try {
                oWard = new HuWard(); HIS.Utils.Helper.SetNullToEmptyString(oWard);
                Data.Add("oWard", oWard);
                
                if (HttpContext.Current.Session["HospitalUnitId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["HospitalUnitId"]);
                    hospitalName = Convert.ToString(HttpContext.Current.Session["hospitalName"]).NullSafeToString();
                }
                Data.Add("HospitalUnitId", id);
                Data.Add("hospitalName", hospitalName);
                Data.Add("IsCreate", "true");

                Data["Pagetitle"] += " :: Create New Ward";
                Data["Notification"] = strNotification;
            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("master\\hospitalunits\\editward.html");
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void EditWard() {
            StringReader stringReader = null;
            string hospitalName = null;
            int id = 0;
            try {
                string wardencrpt = HttpContext.Current.Request["wardencrpt"].NullSafeToString();
                if (!string.IsNullOrEmpty(wardencrpt)) {
                    string warddecript = KeanuMvc.Reference.CryptoString.Decrypt(wardencrpt);
                    string[] strward = warddecript.Split(';');
                    id = Convert.ToInt32(strward[0]);
                } 
            } catch { }

            try {
                oWard = new HuWard(); Utils.Helper.SetNullToEmptyString(oWard);

                string strUri = Global.ServiceUriDictionary["ward"] + id + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.GetJson(strUri);
                oWard = JsonConvert.DeserializeObject<HuWard>(stringReader.ReadToEnd());

                ///Set variable for rendering on view HTML///
                Data.Add("oWard", oWard);

                if (HttpContext.Current.Session["HospitalUnitId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["HospitalUnitId"]);
                    hospitalName = Convert.ToString(HttpContext.Current.Session["hospitalName"]).NullSafeToString();
                }
                Data.Add("HospitalUnitId", id);
                Data.Add("hospitalName", hospitalName);
                Data.Add("IsCreate", "false");

                Data["Pagetitle"] += " :: Edit Ward";
                Data["Notification"] = strNotification;
            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("master\\hospitalunits\\editward.html");
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void SaveWardToService() {
            StringReader stringReader = null;
            string strUri = string.Empty;
           
            try {
                oWard = new HuWard(); Utils.Helper.SetNullToEmptyString(oWard);
                int Id = 0;
                try {
                    Id = Convert.ToInt32(HttpContext.Current.Request["TextboxHospitalUnitId"]);
                } catch { }
                
                oWard.PostHandler<HuWard>();
                oWard.HospitalUnitId = Id;
                strUri = Global.ServiceUriDictionary["ward"] + Global.ServiceUriDictionary["requestformat"];
                if (HttpContext.Current.Request["IsCreate"] == "true") {
                    stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(oWard));
                } else {
                    stringReader = base.RestService.PutJson(strUri, JsonConvert.SerializeObject(oWard));
                }

                oWard = JsonConvert.DeserializeObject<HuWard>(stringReader.ReadToEnd());

                if (oWard != null) {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                } else {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }

                strNotification = "{\"strNotification\": \"" + strNotification + "\"}";

            } catch (Exception ex) {
                strNotification = ex.Message;
            } finally {
                HttpContext.Current.Response.Write(strNotification);
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void DeleteWard() {
            ///Set variables///
            StringReader stringReader = null;
            int id = 0;
            try {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            } catch {}

            try {
                oWard = new HuWard();
                string strUri = Global.ServiceUriDictionary["ward"] + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.DeleteJson(strUri, JsonConvert.SerializeObject(id));

                try {
                    oWard = JsonConvert.DeserializeObject<HuWard>(stringReader.ReadToEnd());
                } catch { }

                if (oWard != null) {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                } else {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }

            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                HttpContext.Current.Response.Write(strNotification);
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void SetWard() {
            HttpContext.Current.Session.Remove("RoomId");
            HttpContext.Current.Session.Remove("RoomName");
            int id = 0;
            string hospitalName = string.Empty;
            string WardName = string.Empty;
            if (HttpContext.Current.Session["WardId"] != null) {
                id = Convert.ToInt32(HttpContext.Current.Session["WardId"]);
                WardName=Convert.ToString(HttpContext.Current.Session["WardName"]);
            } else {
                string wardencrpt = HttpContext.Current.Request["wardencrpt"].NullSafeToString();
                if (!string.IsNullOrEmpty(wardencrpt)) {
                    string warddecript = KeanuMvc.Reference.CryptoString.Decrypt(wardencrpt);
                    string[] strward = warddecript.Split(';');
                    id = Convert.ToInt32(strward[0]);
                    WardName = strward[1];
                    HttpContext.Current.Session.Add("WardId", id);
                    HttpContext.Current.Session.Add("WardName", WardName);
                } 
            }

            try {
                Data["Pagetitle"] = "Hospital Unit Room";
                
                Data.Add("WardId", id);
                Data.Add("WardName", WardName);
                if (HttpContext.Current.Session["HospitalUnitId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["HospitalUnitId"]);
                    hospitalName = Convert.ToString(HttpContext.Current.Session["hospitalName"]).NullSafeToString();
                }
                Data.Add("HospitalUnitId", id);
                Data.Add("hospitalName", hospitalName);
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            } finally {
                RenderFile("master\\hospitalunits\\viewroom.html");
            }
        }

                
        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void TableListRoom() {
            StringReader stringReader = null;
            string strJson = string.Empty;
            string TextboxHospitalUnitId = string.Empty;


            try {
                string WardId = HttpContext.Current.Request["WardId"].NullSafeToString();                
                string TextboxRoomCode = HttpContext.Current.Request["TextboxRoomCode"].NullSafeToString();
                string TextboxRoomName = HttpContext.Current.Request["TextboxRoomName"].NullSafeToString();
                string ssearch = "WardId=" + WardId + ";TextboxRoomCode=" + TextboxRoomCode + ";TextboxRoomName=" + TextboxRoomName;
                ///--------- Sample post to rest and get record as paging-------------/// 
                ///Create paging object///
                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                if (!string.IsNullOrEmpty(ssearch)) {
                    pagingSearch.sSearch = ssearch;
                } else { pagingSearch.sSearch = Convert.ToString(HttpContext.Current.Request["sSearch"]); }

                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);

                string strUri = Global.ServiceUriDictionary["roomwardpaging"] + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                //strJson = stringReader.ReadToEnd();

                try {
                    JObject o = JObject.Parse(stringReader.ReadToEnd());
                    JArray aaData = (JArray)o["aaData"];
                    JArray aaDataModified = new JArray();

                    for (int i = 0; i < aaData.Count(); i++) {
                        JObject oo = JObject.Parse(aaData[i].ToString());
                        oo["roomencrpt"] = KeanuMvc.Reference.CryptoString.Encrypt((oo["Id"] + ";" +
                                             oo["NameValue"]).ToString());
                        aaDataModified.Add(oo);
                    }
                    o["aaData"] = aaDataModified;
                    strJson = JsonConvert.SerializeObject(o);
                } catch { }

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            } finally {
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
                HttpContext.Current.Response.Write(strJson);
            }
        }

        [Authorization(RoleAction = "all")]
        public void CreateHospitalRoom() {
            ///Set variables///
            StringReader stringReader = null;
            string hospitalName = string.Empty;
            try {
                int id = 0; string WardName = string.Empty;
                if (HttpContext.Current.Session["WardId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["WardId"]);
                }
                if (HttpContext.Current.Session["WardName"] != null) {
                    WardName = Convert.ToString(HttpContext.Current.Session["WardName"]);
                }
                Data.Add("WardId", id);
                Data.Add("WardName", WardName);
                
                oHuRoom = new HuRoom(); HIS.Utils.Helper.SetNullToEmptyString(oHuRoom);
                Data.Add("oHuRoom", oHuRoom);
                Data.Add("IsCreate", "true");

                
                if (HttpContext.Current.Session["HospitalUnitId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["HospitalUnitId"]);
                    hospitalName = Convert.ToString(HttpContext.Current.Session["hospitalName"]).NullSafeToString();
                }
                Data.Add("HospitalUnitId", id);
                Data.Add("hospitalName", hospitalName);
                Data["Pagetitle"] += " :: Add Hospital Room";
                Data["Notification"] = strNotification;
            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("master\\hospitalunits\\edithospitalroom.html");
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void EditHospitalRoom() {
            ///Set variables///
            StringReader stringReader = null;
            string hospitalName = string.Empty;
            /// Get id record from querystring
            int id = 0;
            try {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
                string roomencrpt = HttpContext.Current.Request["roomencrpt"].NullSafeToString();
                if (!string.IsNullOrEmpty(roomencrpt)) {
                    string roomdecript = KeanuMvc.Reference.CryptoString.Decrypt(roomencrpt);
                    id = Convert.ToInt32(roomdecript);
                } 
            } catch { }

            try {
                oHuRoom = new HuRoom(); Utils.Helper.SetNullToEmptyString(oHuRoom);
                string strUri = Global.ServiceUriDictionary["room"] + id + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.GetJson(strUri);
                oHuRoom = JsonConvert.DeserializeObject<HuRoom>(stringReader.ReadToEnd());

                string WardName = string.Empty;
                if (HttpContext.Current.Session["WardId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["WardId"]);
                }
                if (HttpContext.Current.Session["WardName"] != null) {
                    WardName = Convert.ToString(HttpContext.Current.Session["WardName"]);
                }
                Data.Add("WardId", id);
                Data.Add("WardName", WardName);

                ///Set variable for rendering on view HTML///
                Data.Add("oHuRoom", oHuRoom);
                Data.Add("IsCreate", "false");
                
                if (HttpContext.Current.Session["HospitalUnitId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["HospitalUnitId"]);
                    hospitalName = Convert.ToString(HttpContext.Current.Session["hospitalName"]).NullSafeToString();
                }
                Data.Add("HospitalUnitId", id);
                Data.Add("hospitalName", hospitalName);
                Data["Pagetitle"] += " :: Edit Hospital Room";
                Data["Notification"] = strNotification;
            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("master\\hospitalunits\\edithospitalroom.html");
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void SaveHospitalRoom() {
            StringReader stringReader = null;
            string strUri = string.Empty;
            try {
                oHuRoom = new HuRoom(); Utils.Helper.SetNullToEmptyString(oHuRoom);
                int Id = 0;
                try {
                    Id = Convert.ToInt32(HttpContext.Current.Request["WardId"]);
                } catch { }

                oHuRoom.PostHandler<HuRoom>();
                oHuRoom.WardId = Id;
                //flag
                strUri = Global.ServiceUriDictionary["room"] + Global.ServiceUriDictionary["requestformat"];
                if (HttpContext.Current.Request["IsCreate"] == "true") {
                    stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(oHuRoom));
                } else {
                    stringReader = base.RestService.PutJson(strUri, JsonConvert.SerializeObject(oHuRoom));
                }
                try {
                    oHuRoom = JsonConvert.DeserializeObject<HuRoom>(stringReader.ReadToEnd());
                } catch {

                }
                if (oHuRoom != null) {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                } else {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }

                strNotification = "{\"strNotification\": \"" + strNotification + "\"}";
                HttpContext.Current.Response.Write(strNotification);

            } catch (Exception ex) {
                strNotification = ex.Message;
            } finally {

                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void DeleteHospitalRoom() {
            ///Set variables///
            StringReader stringReader = null;
            int id = 0;
            try {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            } catch { }

            try {
                string strUri = Global.ServiceUriDictionary["room"] + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.DeleteJson(strUri, JsonConvert.SerializeObject(id));

                try {
                    oHuRoom = JsonConvert.DeserializeObject<HuRoom>(stringReader.ReadToEnd());
                } catch { }

                if (oHuRoom != null) {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                } else {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }

            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                HttpContext.Current.Response.Write(strNotification);
                base.RestService.Dispose();
            }
        }

        //Flag
        [Authorization(RoleAction = "all")]
        public void SetHospitalRoom() {
            int id = 0;
            string hospitalName = string.Empty;
            string WardName = string.Empty;
            string RoomName = string.Empty;
            if (HttpContext.Current.Session["RoomId"] != null) {
                id = Convert.ToInt32(HttpContext.Current.Session["RoomId"]);
                RoomName = Convert.ToString(HttpContext.Current.Session["RoomName"]);
            } else {
                string roomencrpt = HttpContext.Current.Request["roomencrpt"].NullSafeToString();
                if (!string.IsNullOrEmpty(roomencrpt)) {
                    string roomdecript = KeanuMvc.Reference.CryptoString.Decrypt(roomencrpt);
                    string[] strroom = roomdecript.Split(';');
                    id = Convert.ToInt32(strroom[0]);
                    RoomName = strroom[1];
                    HttpContext.Current.Session.Add("RoomId", id);
                    HttpContext.Current.Session.Add("RoomName", RoomName);
                }
            }

            try {
                Data["Pagetitle"] = "Hospital Unit Bed";
                Data.Add("RoomId", id);
                Data.Add("RoomName", RoomName);

                if (HttpContext.Current.Session["WardId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["WardId"]);
                    WardName = Convert.ToString(HttpContext.Current.Session["WardName"]).NullSafeToString();
                    Data.Add("WardId", id);
                    Data.Add("WardName", WardName);
                }
                
                if (HttpContext.Current.Session["HospitalUnitId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["HospitalUnitId"]);
                    hospitalName = Convert.ToString(HttpContext.Current.Session["hospitalName"]).NullSafeToString();
                    Data.Add("HospitalUnitId", id);
                    Data.Add("hospitalName", hospitalName);
                }
                
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            } finally {
                RenderFile("master\\hospitalunits\\viewbed.html");
            }
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public void TableListBed() {
            StringReader stringReader = null;
            string strJson = string.Empty;
            string TextboxHospitalUnitId = string.Empty;


            try {
                string RoomId = HttpContext.Current.Request["RoomId"].NullSafeToString();
                string TextboxBedName = HttpContext.Current.Request["TextboxBedName"].NullSafeToString();
                string ssearch = "RoomId=" + RoomId + ";TextboxBedName=" + TextboxBedName;
                ///--------- Sample post to rest and get record as paging-------------/// 
                ///Create paging object///
                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                if (!string.IsNullOrEmpty(ssearch)) {
                    pagingSearch.sSearch = ssearch;
                } else { pagingSearch.sSearch = Convert.ToString(HttpContext.Current.Request["sSearch"]); }

                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);

                string strUri = Global.ServiceUriDictionary["bedroompage"] + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                //strJson = stringReader.ReadToEnd();

                try {
                    JObject o = JObject.Parse(stringReader.ReadToEnd());
                    JArray aaData = (JArray)o["aaData"];
                    JArray aaDataModified = new JArray();

                    for (int i = 0; i < aaData.Count(); i++) {
                        JObject oo = JObject.Parse(aaData[i].ToString());
                        oo["bedencrpt"] = KeanuMvc.Reference.CryptoString.Encrypt(oo["Id"].ToString());
                        aaDataModified.Add(oo);
                    }
                    o["aaData"] = aaDataModified;
                    strJson = JsonConvert.SerializeObject(o);
                } catch { }

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            } finally {
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
                HttpContext.Current.Response.Write(strJson);
            }
        }

        [Authorization(RoleAction = "all")]
        public void CreateBed() {
            ///Set variables///
            StringReader stringReader = null;
            string hospitalName = string.Empty;
            string WardName = string.Empty;
            string RoomName = string.Empty;
            try {
                int id = 0; 
                if (HttpContext.Current.Session["RoomId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["RoomId"]);
                }
                if (HttpContext.Current.Session["RoomName"] != null) {
                    RoomName = Convert.ToString(HttpContext.Current.Session["RoomName"]);
                }
                Data.Add("RoomId", id);
                Data.Add("RoomName", RoomName);

                oHuBed = new HuBed(); HIS.Utils.Helper.SetNullToEmptyString(oHuBed);
                Data.Add("oHuBed", oHuBed);
                Data.Add("IsCreate", "true");

                if (HttpContext.Current.Session["WardId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["WardId"]);
                    WardName = Convert.ToString(HttpContext.Current.Session["WardName"]).NullSafeToString();
                    Data.Add("WardId", id);
                }
                Data.Add("WardName", WardName);

                if (HttpContext.Current.Session["HospitalUnitId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["HospitalUnitId"]);
                    hospitalName = Convert.ToString(HttpContext.Current.Session["hospitalName"]).NullSafeToString();
                    Data.Add("HospitalUnitId", id);
                }
                Data.Add("hospitalName", hospitalName);
                
                Data["Pagetitle"] += " :: Add Hospital Room";
                Data["Notification"] = strNotification;
            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("master\\hospitalunits\\editbed.html");
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void EditBed() {
            ///Set variables///
            StringReader stringReader = null;
            string hospitalName = string.Empty;
            string WardName = string.Empty;
            string RoomName = string.Empty;
            /// Get id record from querystring
            int id = 0;
            try {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
                string bedencrpt = HttpContext.Current.Request["bedencrpt"].NullSafeToString();
                if (!string.IsNullOrEmpty(bedencrpt)) {
                    string beddecript = KeanuMvc.Reference.CryptoString.Decrypt(bedencrpt);
                    id = Convert.ToInt32(beddecript);
                }
            } catch { }

            try {
                oHuBed = new HuBed(); Utils.Helper.SetNullToEmptyString(oHuBed);
                string strUri = Global.ServiceUriDictionary["bed"] + id + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.GetJson(strUri);
                oHuBed = JsonConvert.DeserializeObject<HuBed>(stringReader.ReadToEnd());
                Data.Add("oHuBed", oHuBed);
                Data.Add("IsCreate", "false");

                if (HttpContext.Current.Session["RoomId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["RoomId"]);
                    Data.Add("RoomId", id);
                }
                if (HttpContext.Current.Session["RoomName"] != null) {
                    RoomName = Convert.ToString(HttpContext.Current.Session["RoomName"]);
                }
                Data.Add("RoomName", RoomName);

                if (HttpContext.Current.Session["WardId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["WardId"]);
                    Data.Add("WardId", id);
                }
                if (HttpContext.Current.Session["WardName"] != null) {
                    WardName = Convert.ToString(HttpContext.Current.Session["WardName"]);
                }
                Data.Add("WardName", WardName);
                
               if (HttpContext.Current.Session["HospitalUnitId"] != null) {
                    id = Convert.ToInt32(HttpContext.Current.Session["HospitalUnitId"]);
                    hospitalName = Convert.ToString(HttpContext.Current.Session["hospitalName"]).NullSafeToString();
                }
                Data.Add("HospitalUnitId", id);
                Data.Add("hospitalName", hospitalName);

                Data["Pagetitle"] += " :: Edit Bed";
                Data["Notification"] = strNotification;
            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                RenderFile("master\\hospitalunits\\editbed.html");
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void SaveBedToService() {
            StringReader stringReader = null;
            string strUri = string.Empty;
            try {
                oHuBed = new HuBed(); Utils.Helper.SetNullToEmptyString(oHuBed);
                int Id = 0;
                try {
                    Id = Convert.ToInt32(HttpContext.Current.Request["RoomId"]);
                } catch { }

                oHuBed.PostHandler<HuBed>();
                oHuBed.RoomId = Id;
                //flag
                strUri = Global.ServiceUriDictionary["bed"] + Global.ServiceUriDictionary["requestformat"];
                if (HttpContext.Current.Request["IsCreate"] == "true") {
                    stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(oHuBed));
                } else {
                    stringReader = base.RestService.PutJson(strUri, JsonConvert.SerializeObject(oHuBed));
                }
                try {
                    oHuBed = JsonConvert.DeserializeObject<HuBed>(stringReader.ReadToEnd());
                } catch {

                }
                if (oHuBed != null) {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                } else {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }

                strNotification = "{\"strNotification\": \"" + strNotification + "\"}";
                HttpContext.Current.Response.Write(strNotification);

            } catch (Exception ex) {
                strNotification = ex.Message;
            } finally {

                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "all")]
        public void DeleteBed() {
            ///Set variables///
            StringReader stringReader = null;
            oHuBed = new HuBed(); 
            int id = 0;
            try {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            } catch { }

            try {
                string strUri = Global.ServiceUriDictionary["bed"] + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.DeleteJson(strUri, JsonConvert.SerializeObject(id));

                try {
                    oHuBed = JsonConvert.DeserializeObject<HuBed>(stringReader.ReadToEnd());
                } catch { }

                if (oHuBed != null) {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                } else {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }

            } catch (Exception ex) {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            } finally {
                HttpContext.Current.Response.Write(strNotification);
                base.RestService.Dispose();
            }
        }

        //End Flag

        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public string GetHuLobRel(string name, KeanuMvc.Reference.ClientRestService clientService, string Selected, string hospitalId, string classAttribute, bool isRequired) {
            string strResult = string.Empty;

            string strUri = Global.ServiceUriDictionary["hospitallob"] + hospitalId + Global.ServiceUriDictionary["requestformat"];
            StringReader stringReader = clientService.GetJson(strUri);
            List<HuLobRel> listOption = JsonConvert.DeserializeObject<List<HuLobRel>>(stringReader.ReadToEnd());

            List<object[]> listDropdown = new List<object[]>();
            if (isRequired) {
                listDropdown.Add(new object[] { "", "--Select--" });
            } else {
                listDropdown.Add(new object[] { "", "*" });
            }
            
            foreach (HuLobRel item in listOption) {
                listDropdown.Add(new object[] { item.Id, item.LineOffBusinessUnitID.ToEnum<HISModels.Enums.StaticVariables.eLOB>().ToString() });
            }

            strResult = KeanuMvc.Reference.Html.DropDown(name, classAttribute, listDropdown, Selected);
            listDropdown.Clear();
            listDropdown = null;
            stringReader.Dispose();
            return strResult;
        }

    }
}